package br.com.searchpeople.activity;

import br.com.searchpeople.R;
import br.com.searchpeople.asynctask.RegisterTask;
import br.com.searchpeople.database.SingletonAccountBD;
import br.com.searchpeople.model.UserModel;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SignUpActivity extends Activity implements OnClickListener{
	private final static String ENDPOINT_URL = "http://10.5.18.163:3000/api/v1/registrations";
	//private final static String ENDPOINT_URL = "http://10.0.2.2:3000/api/v1/registrations";
	//private final static String ENDPOINT_URL = "http://192.168.1.103:3000/api/v1/sessions.json";
	//private final static String ENDPOINT_URL = "http://10.5.18.163:3000/api/v1/sessions.json";
	private Button signUpAndLogin;
	private String nickname;
	private String user_email;
	private String password;
	private String confirm_password;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        
        signUpAndLogin = (Button) findViewById(R.id.link_to_login);
		signUpAndLogin.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		nickname = ((EditText) findViewById(R.id.edittext_signup_nickname)).getText().toString();
        user_email = ((EditText) findViewById(R.id.edittext_signup_user_email)).getText().toString();
		password = ((EditText) findViewById(R.id.edittext_signup_password)).getText().toString();
		confirm_password = ((EditText) findViewById(R.id.edittext_signup_confirm_password)).getText().toString();
		if (user_email.length() == 0 || nickname.length() == 0 || password.length() == 0 || confirm_password.length() == 0) {
	        
	        createSpecificNotificationToast("Por favor, preenchar todos os campos!");
	        return;
	    } else {
	        if (!password.equals(confirm_password)) {
	            createSpecificNotificationToast("A senha e o confirmar senha não combinam!");
	            return;
	        } else {
	            RegisterTask registerTask = new RegisterTask(SignUpActivity.this,user_email, password, confirm_password, nickname);
	            registerTask.setMessageLoading("Registrando novo Usuario...");
	            registerTask.execute(ENDPOINT_URL);
	        }
	    }		
	}
	
	private void createSpecificNotificationToast(String arg0) {
		Context context = getApplicationContext();
		CharSequence text = arg0;
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}

}